﻿using Commission.DAL;
using Commission.Models;
using Commission.ViewModels;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Commission.Controllers
{
    public class HomeController : BaseController
    {
        private CustomerRepository db = new CustomerRepository(new SchoolContext());

        public ActionResult Index()
        {
            if (Request.IsAuthenticated)
            {
                if (User.IsInRole("Admin"))
                {

                }
                else
                {
                    return RedirectToAction("Information", "Customer");
                }
            }

            return View();
        }

        public ActionResult About()
        {
            /*
            Customer login = db.GetCustomerByUserName(User.Identity.GetUserName());
            List<SponsorCommission> commissions = db.GetSponsorCommission(login.CustomerTypeID);

            DateTime startTime = DateTime.Now;
            DateTime endTime = login.ValidDate;
            TimeSpan span = endTime - startTime;

            if (commissions.Count > 0)
            {
                endTime = login.UpgradeDate.AddDays(commissions[0].UpgradeDays);
                span = endTime - startTime;
                ViewBag.UpgradeTime1 = span.Days.ToString("D2")
                    + " " + span.Hours.ToString("D2")
                    + " " + span.Minutes.ToString("D2")
                    + " " + span.Seconds.ToString("D2");

                if (commissions.Count > 1)
                {
                    endTime = login.UpgradeDate.AddDays(commissions[1].UpgradeDays);
                    span = endTime - startTime;
                    ViewBag.UpgradeTime2 = span.Days.ToString("D2")
                        + " " + span.Hours.ToString("D2")
                        + " " + span.Minutes.ToString("D2")
                        + " " + span.Seconds.ToString("D2");
                }
            }

            endTime = login.ValidDate;
            if (startTime.CompareTo(endTime) < 0) { 
                span = endTime.Subtract(startTime);
                ViewBag.ValidTime = span.Days.ToString("D2")
                    + " " + span.Hours.ToString("D2")
                    + " " + span.Minutes.ToString("D2")
                    + " " + span.Seconds.ToString("D2");
            }*/

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }

        public ActionResult Chart()
        {
            string url = Url.Content("~/");
            return Redirect(url + "Chart/OrgChart.aspx");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

    }
}